package com.haitonghop.modules.news.rssreader.model;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

public class FeedEntry {

	private String title;
	private String link;
	private String summary;
	private String image;
	private String description;
	private String enclosure;
	private Date pubdate;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getImage() {
		if (image == null) {
			if (enclosure != null && enclosure.length() > 0 && enclosure.startsWith("http")) {
				image = enclosure;
			} else if (description != null && description.length() > 0) {
				description = description.replaceAll("&lt;", "<");
				description = description.replaceAll("&gt;", ">");
				description = description.replaceAll("'", "\"");
				Log.i("IMAGE", description);
				if (link.indexOf("baohay") != -1) {
					Pattern p = null;
					Matcher m = null;
					String word0 = null;
					String word1 = null;
					try {
						p = Pattern.compile(".*<img[^>]*src= \"([^\"]*)", Pattern.CASE_INSENSITIVE);
						m = p.matcher(description);
						while (m.find()) {
							word0 = m.group(1);
							image = (word0.toString());
						}
					} catch (Exception e) {

					}
				} else {
					Pattern p = null;
					Matcher m = null;
					String word0 = null;
					String word1 = null;
					try {
						p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)", Pattern.CASE_INSENSITIVE);
						m = p.matcher(description);
						while (m.find()) {
							word0 = m.group(1);
							image = (word0.toString());
						}
					} catch (Exception e) {

					}
				}
			}
		}

		if (image != null) {
			if (!image.startsWith("http://") && link.indexOf("thanhnien") != -1) {
				image = "http://www.thanhnien.com.vn" + image;
			} else if (!image.startsWith("http://") && link.indexOf("danviet") != -1) {
				image = "http://images.danviet.vn/ThumbImages" + image;
			}
		}
		Log.i("FeedEntry", "" + image);
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Date getPubdate() {
		return pubdate;
	}

	public void setPubdate(Date pubdate) {
		this.pubdate = pubdate;
	}

	@Override
	public String toString() {
		return "Entry [title=" + title + ", pubdate=" + pubdate + ", link=" + link + "]";
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getEnclosure() {
		return enclosure;
	}

	public void setEnclosure(String enclosure) {
		Log.i("FeedEntry", "enclosure > " + enclosure);
		this.enclosure = enclosure;
	}

}